// JavaScript Document
((function(a){
	//插件本体
	var uploader = function(opts){
		this.elebox = a.createElement('div');
		this.card = new a.card({
			format:['head','body','foot'],
			tips:'视频上传',
			width:'600px',
			height:'460px',
			autoCreate:false,
		});
		this.format = {},
		this.files = null;//文件列表所有的信息
		
		//创建一个视频类型的UI
		this.createLocalVideoUI = function(file){
			var ths = this;
			file.ID = 'ui_' + Math.ceil( Math.random() * Math.pow(10,5));
			file.UI = function(){
				//card.attr('src',this.getTmpurl());
				var box = a('<div data-id="'+file.ID+'" style="display: inline-block;position: relative;width: 180px;height: 101.25px;margin: 5px;"></div>');
				var process = a('<div data-template-id="process-'+file.ID+'" style="position: absolute;bottom: 5px;left: 5px;right: 5px;height: 3px;background-color: #fff;"></div>');
				var rpocessbar = a('<div data-template-id="processbar-'+file.ID+'" style="width:0%;height: 100%;background-color: #d00;"></div>');
				//a(box).append(card);
				a(box).append(process);
				a(process).html(rpocessbar);
				return box;
			}
			this.card.format.body.append(file.UI());
		}
		
		//创建一个其他类型的UI
		this.createLocalOtherUI = function(){
			$Q.alert('isOther');	
		}
		
		
		//创建一个视频的本地UI
		this.createLocalUI = function(file){
			if(file.isVideo()){
				//创建视频类型的文件
				this.createLocalVideoUI(file);
			}else{
				//创建其他类型的文件
				this.createLocalOtherUI(file);	
			}
		}
		
		//创建载入视频的按钮
		this.createLoadvideoBtn = function(){
			var ths = this;
			this.format.uploaderbtn = a(a.createElement('button'));
			this.format.uploaderbtn.attr('type','button');
			this.format.uploaderbtn.text('加载视频');
			this.format.uploaderbtn.css({
				'margin':'5px',	
			});
			this.format.uploaderbtn.click(function(){
				ths.files = new $Q.files();
				ths.files.createBynone(function(){
					this.each(function(i,itm){
						ths.createLocalUI(itm);
					});
				});
			});
			this.card.format.foot.append(this.format.uploaderbtn);
		}
		
		//创建提交视频的按钮
		this.createUploadvideoBtn = function(){
			var ths = this;
			this.format.uploaderbtn = a(a.createElement('button'));
			this.format.uploaderbtn.attr('type','button');
			this.format.uploaderbtn.text('上传视频');
			this.format.uploaderbtn.css({
				'margin':'5px',	
			});
			this.format.uploaderbtn.click(function(){
				if(!ths.files){ 
					return $Q.msg({
						text:'请先加载文件',	
						position:'t',
					});
				}
				var uploadfile = function(file){
					file.onCompressStart = function(){
						$Q.loading(true);
						$Q.msg({
							text:'视频【'+file.file.name+'】压缩开始',	
							position:'t',
						});
					}
					file.onCompressReady = function(){
						$Q.loading(false);
						$Q.msg({
							text:'视频【'+file.file.name+'】已获取内容，开始压缩',	
							position:'t',
						});	
					}
					file.onCompressProgress = function(index){
						$Q.msg({
							text:'视频【'+file.file.name+'】压缩第'+index+'片段',	
							position:'t',
						});	
					}
					file.onCompressEnd = function(){
						$Q.msg({
							text:'视频【'+file.file.name+'】压缩完成',	
							position:'t',
						});	
					}
					file.onUploadStart = function(){
						var bar = $Q(ths.card.format.body).find('[data-template-id="processbar-'+file.ID+'"]');
						var box = $Q(ths.card.format.body).find('[data-template-id="process-'+file.ID+'"]');
						bar.css('width','0%');
					}
					file.onUploadEnd = function(){
						var bar = $Q(ths.card.format.body).find('[data-template-id="processbar-'+file.ID+'"]');
						var box = $Q(ths.card.format.body).find('[data-template-id="process-'+file.ID+'"]');
						bar.css('width','100%');
						//上传成功
						//判断是否有下一个文件
						if(filenext = ths.files.hasNeedUpload()){
							uploadfile(filenext);
						}else{
							console.log('全部上传完成');
						}
					}
					file.onUploadErr = function(){
						$Q.msg({
							text:'视频【'+file.file.name+'】上传失败',	
							position:'t',
						});	
					}
					file.onUploadSecStart = function(sec){
						
					}
					file.onUploadSecEnd = function(sec){
						var bar = $Q(ths.card.format.body).find('[data-template-id="processbar-'+file.ID+'"]');
						var max = file.getSecnum();
						var num = file.getSecUploadednum();
						var w = num / max * 100;
						bar.css('width',w+'%');
					}
					file.onUploadSecErr = function(sec){
						var bar = $Q(ths.card.format.body).find('[data-template-id="processbar-'+file.ID+'"]');
						var max = file.getSecnum();
						var num = file.getSecUploadednum();
						var w = num / max * 100;
						bar.css('width',w+'%');
					}
					//压缩，看是否压缩完成
					file.compress(function(){
						//压缩完成之后，进行上传步骤的相关操作
						this.upload();	
					});
				}
				var file = ths.files.first();
				uploadfile(file);
			});
			this.card.format.foot.css({
				'text-align': 'right',
				'padding-right': '1em',
				'margin-left': '1em',
				'margin-right': '1em',
			});
			this.card.format.foot.append(this.format.uploaderbtn);
		}
		
		//创建UI界面信息
		this.createUI = function(){
			this.createLoadvideoBtn();
			this.createUploadvideoBtn();
			this.card.create();
		}
		
		//创建程序
		this.create = function(){
			this.createUI();
		}
		//检测后端服务是否正常
		//使用post的方式进行
		this.checkWebserver = function(){
			var config = a.config('UPLOAD');
			a.ajax({
				url:config.ACTION,
				data:config.PARAMS,
				type:'post',
				dataType:'json',
				success:function(rs){
					if(rs.status==1){
						
					}else{
						$Q.alert(rs.message);	
					}
				},
				error:function(){
					$Q.alert('上传控件初始化失败，请检查服务器端是否能正常返回');	
				},
			});
		}
		
		//检测后端是否可用
		this.checkWebserver();
		this.create();//创建代码
		
	};
	a.plugIn.register('videoUploader',uploader);
})(this.kuoqiJs))